<?php

    class W_user_mod extends Model{

        function W_user_mod(){

            parent::Model();

        }

        

        function getData($field, $tabel, $where='',$group_by='') {

            $this->db->select($field);

            if ($where!='') {

                $this->db->where($where);

            }

            if ($group_by!='') {

                $this->db->group_by($group_by);

            }

            $query = $this->db->get($tabel);

            if ($query->num_rows() > 0) {

                if ($row = $query->row_array()) {

                    return $row[$field];

                } else {

                    return false;

                }

            } else {

                return false;

            }

        }

        

        

        

        function list_user($where="", $num=0, $offset=0, $ro=false) {

            

            $this->db->from('user');

             
            $this->db->where('namauser != ','admin');
            

            if ($where!='') {

                $this->db->where($where);

            }

            $this->db->order_by('namauser','asc');

            if ($num > 0) {

                $this->db->limit($num, $offset);

            }

            $query = $this->db->get();

            $s = "

              <table align=\"center\" cellspacing=\"1\" cellpadding=\"2\" class=\"tbl\">

                <tbody>

                  <tr>

                    <th class='number'>No</th>

                    <th>Nama</th>

                    <th>Nama Lengkap</th>

                    <th>Akses Unit Kerja</th>                    

                    <th>Grup</th>

                    ";

            if ($ro==false) {

                $s .= " 

                        <th colspan='2'> Aksi </th>";

            }



            $s .= "</tr>

            ";

            if ($num > 0) {

                $i = ($num * ($offset/$num)) + 1;

            } else {

                $i = 1;

            }



            

            

            foreach ($query->result() as $row) {

                

                

                if ($i % 2 == 1 ) {

                    $kelas = "event2";

                } else {

                    $kelas = "event";

                }

               

                    

                $grup = $this->getData('nama', 'grup', 'idspg_grup = '.$row->id_grup);
                
                //$akses = (!empty($row->id_akses))?$this->getData('nama', 'unit_kerja', 'idspg_unit_kerja = '.$row->id_akses.''):"All";
                              

                $s .= "

              <tr class=\"$kelas\">

                

                <td><div align=\"right\">$i.</div></td>                

                <td><div>".$row->namauser."</div></td>

                <td><div>".$row->nama_asli."</div></td>
                
                <td><div>".combo_instansi(base64_encode($row->namauser),$row->id_akses,'All','akses')."</div></td>

                <td><div align=\"center\">$grup</div></td>

                

                ";

                if ($ro==false) {

                    $s .= "

                    

                    <td><div align='center'> <a href='".base_url()."index.php/main_user/edit_user/".$row->namauser."' title=\"Ubah Data\" class=\"edit\"><img src='".base_url()."images/edit.gif' border='0'></a></div></td>   

                    <td><div align='center'> <a href=\"javascript:deleteRow('".$row->namauser."')\"><img src='".base_url()."images/del.gif' border='0'></a></div></td>";

                    

                }

                $s .= "

              </tr>";



                $i++;

            }

            $s .= "</table>";

            return $s;

        }



        

        function entry_user($url,$namauser='',$nama_lengkap='',$passwd='',$id_grup='') {

            

            if ($namauser =='')

                $box = "<input name='namauser' id='namauser' type='text' size='15' value='$namauser' class='required' title='Nama harus diisi' />";

            else

                $box = "<input name='namauser' id='namauser' type='hidden' size='15' value='$namauser' /> $namauser";                                    



            $ret = "<fieldset>

            <form id='tambahuser' method='post' action='".base_url()."index.php/main_user/$url'>

              <table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\">      

                  <tr>

                    <td class=\"kolom\" valign=\"top\">Username </td>

                    <td>:</td>

                    <td>$box</td>

                  </tr>

                  <tr>

                    <td class=\"kolom\" valign=\"top\">Nama Lengkap </td>

                    <td>:</td>

                    <td><input name='nama_lengkap' id='nama_lengkap' type='text' size='25' value='$nama_lengkap'/></td>

                  </tr>

                  <tr>

                    <td class=\"kolom\" valign=\"top\">Password </td>

                    <td>:</td>

                    <td><input name='passwd' id='passwd' type='password' size='15'  /></td>

                  </tr>

                  <tr>

                    <td class=\"kolom\" valign=\"top\">Ulangi Password </td>

                    <td>:</td>

                    <td><input name='passwd2' id='passwd2' type='password' size='15' /></td>

                  </tr>

                  <tr>

                    <td class=\"kolom\" valign=\"top\">Grup </td>

                    <td>:</td>

                    <td>".combo_jenis_admin('jenis_admin',$id_grup,'')."</td>

                  </tr>

               </table>

               <p>

                  <div >

                          <input class=\"button\" type=\"button\" value=\"<< Kembali\" name=\"btn_kembali\" id=\"btn_kembali\" />&nbsp;

                          <input class=\"button\" type=\"submit\" value=\"Simpan\" name=\"simpan\" />

                  </div>

               </p>

                </form></fieldset>";

                return $ret;

        }

        

        function simpan_user($namauser,$nama_lengkap,$passwd,$id_grup,$is_update=FALSE) {

            $arr['namauser'] = $namauser;            

            $arr['nama_asli'] = $nama_lengkap;

            if ($passwd!='') {

                $arr['passwd'] = $passwd;

            }

            $arr['id_grup'] = $id_grup;  

            

            if ($is_update==FALSE) {

                $this->db->insert('user', $arr);

            } else {

                $this->db->where("namauser", $namauser);

                $this->db->update('user', $arr);

            }

        }

        

        function list_grup($where="", $num=0, $offset=0, $ro=false) {

            

            $this->db->from('grup');

             

            

            if ($where!='') {

                $this->db->where($where);

            }

            

            if ($num > 0) {

                $this->db->limit($num, $offset);

            }

            $query = $this->db->get();

            $s = "

              <table align=\"center\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\" class=\"tbl\">

                <tbody>

                  <tr>

                    <th class='number'>No</th>

                    <th>Nama Grup</th>                    



                    ";

            if ($ro==false) {

                $s .= " <th colspan='2'> Aksi </th>";

            }



            $s .= "</tr>

            ";

            if ($num > 0) {

                $i = ($num * ($offset/$num)) + 1;

            } else {

                $i = 1;

            }



 

            foreach ($query->result() as $row) {

                

                

                if ($i % 2 == 1 ) {

                    $kelas = "event2";

                } else {

                    $kelas = "event";

                }

  

                              

                $s .= "

              <tr class=\"$kelas\" id=\"row-".$row->idspg_grup."\" >

                <td><div align=\"right\">$i.&nbsp;&nbsp;&nbsp;</div></td>

                <td><div>".$row->nama."</div></td>



                

                ";

                if ($ro==false) {

                    $s .= "



                   <!-- <td><div align=\"center\"><input type=\"checkbox\" id=\"".$row->idspg_grup."\" value=\"".$row->idspg_grup."\" /></div></td> -->

                    <td><div align='center'> <a href='".base_url()."index.php/main_user/edit_grup/".$row->idspg_grup."' title=\"Ubah Data\" class=\"edit\"><img src='".base_url()."images/edit.gif' border='0'></a></div></td>   

                    <td><div align='center'><!--<a href=\"javascript:konfirmasi(".$row->idspg_grup.")\" title=\"Hapus Data\" class=\"delete\"> --> <a href=\"javascript:deleteRow(".$row->idspg_grup.")\"><img src='".base_url()."images/del.gif' border='0'></a></div></td>";

                    

                    

                }

                $s .= "

              </tr>";



                $i++;

            }

            $s .= "</table>";

            return $s;

        }

        

        function entry_grup($url='',$nama='', $id=0,$edit=''){

            $ret = "<fieldset>

            

            <form method='post' action='$url' id='grup'>

               <table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\"> 

                  

                  <tr>

                    <td class=\"kolom\" valign=\"top\">Nama  </td>

                    <td>:</td>

                    <td><input name='nama' id='nama' type='text' size='15' value='$nama'  />

                        <input name='id' id='id' type='hidden' size='15' value='$id'  /></td>

                  </tr>

                  <tr>

                    <td > </td>

                    <td> </td>

                    <td><input class=\"button\" type=\"button\" value=\"<< Kembali\" name=\"btn_kembali\" id=\"btn_kembali\" />&nbsp;

                          <input class=\"button\" type=\"submit\" value=\"Simpan\" name=\"simpan\" />

                  </tr>

                  </table>

                  

                          

                          

                </form></fieldset>";

                return $ret;

        }

        

        

        function simpan_grup($table,$nama,$id=0,$is_update=FALSE) {           

            $arr['nama'] = $nama;            

            if ($is_update==FALSE) {

                $this->db->insert($table, $arr);

            } else {

                $this->db->where("idspg_grup", $id);

                $this->db->update($table, $arr);

            }

        }

        

        

    }

?>